// Copyright 2007 Google Inc. All Rights Reserved.

package com.google.jessewilson.codesearch.googlecodesearch;

import com.google.jessewilson.codesearch.api.SearchProvider;
import com.google.jessewilson.codesearch.api.spi.SearchProviderParameters;
import com.google.jessewilson.codesearch.io.jdom.EasyElement;
import com.google.jessewilson.codesearch.io.jdom.Externalizer;

import org.jdom.Element;

/**
 * Extra metadata for the {@link GcsSearchProviderBackend}.
 *
 * @author jessewilson
 */
class GcsSearchProviderParameters implements SearchProviderParameters {

  private final String fileTypes;
  private final String packages;
  private final boolean caseSensitive;
  private final boolean convertToRegex;

  public GcsSearchProviderParameters(String fileTypes, String packages,
      boolean caseSensitive, boolean convertToRegex) {
    assert(fileTypes != null);
    assert(packages != null);
    this.fileTypes = fileTypes;
    this.packages = packages;
    this.caseSensitive = caseSensitive;
    this.convertToRegex = convertToRegex;
  }

  public GcsSearchProviderParameters() {
    this("", "", false, true);
  }

  public String getFileTypes() {
    return fileTypes;
  }

  public String getPackages() {
    return packages;
  }

  public boolean isCaseSensitive() {
    return caseSensitive;
  }

  public boolean convertToRegex() {
    return convertToRegex;
  }

  public SearchProvider getSearchProvider() {
    return new GcsSearchProvider();
  }

  public static class MyExternalizer implements Externalizer<GcsSearchProviderParameters> {

    private static final String CONVERT_TO_REGEX = "convertToRegex";
    private static final String CASE_SENSITIVE = "caseSensitive";
    private static final String PACKAGES = "packages";
    private static final String FILE_TYPES = "fileTypes";

    public GcsSearchProviderParameters readExternal(Element element) {
      EasyElement easyElement = new EasyElement(element);
      String fileTypes = easyElement.getString(FILE_TYPES, "");
      String packages = easyElement.getString(PACKAGES, "");
      boolean caseSensitive = easyElement.getBoolean(CASE_SENSITIVE, false);
      boolean covertToRegex = easyElement.getBoolean(CONVERT_TO_REGEX, false);
      return new GcsSearchProviderParameters(fileTypes, packages, caseSensitive, covertToRegex);
    }

    public void writeExternal(Element element, GcsSearchProviderParameters queryExtra) {
      EasyElement easyElement = new EasyElement(element);
      easyElement.putString(FILE_TYPES, queryExtra.fileTypes);
      easyElement.putString(PACKAGES, queryExtra.packages);
      easyElement.putBoolean(CASE_SENSITIVE, queryExtra.caseSensitive);
      easyElement.putBoolean(CONVERT_TO_REGEX, queryExtra.convertToRegex);
    }
  }
}
